package com.abel.bigwater.data.kente

import com.abel.bigwater.data.BcdString
import com.alibaba.fastjson.JSON
import io.netty.buffer.Unpooled
import org.joda.time.DateTime
import org.junit.Assert
import org.junit.Test
import org.slf4j.LoggerFactory

class KentePacketTests {
    @Test
    fun testParseDemo() {
        val str = """41 54 2B 44 61 74 61 3D 0F A1 9B 98 00 49 C6 33 01 64 13 01 00 02 AB
5C 42 11 7B B5 3E 56 FF 5D 3E E5 58 02 00 00 00 00 00 1F 02 00 00 05 05
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
03 75"""

        Unpooled.buffer().apply {
            writeBytes(BcdString.fromBcdString(str).hexArray)

            val p = KentePacket.parse(this)

            lgr.info("parsed: ${JSON.toJSONString(p, true)}")
            p?.forEach {
                assert(DateTime(2017, 3, 13, 7, 5).equals(DateTime(it.sampleTime)))
                Assert.assertEquals(153829.543, it.forwardReading!!, 1E-5)
                Assert.assertEquals("10001313", it.rtuId.toString())
            }
        }

    }

    @Test
    fun testDatagram() {
        val str = """ADDRESS:001 NAME:10001062 FTOTAL:0000417679.1230M3 RTOTAL:0000000074.2690M3 FLOW:+39.9200M3/h SPEED:0.3529m/s TUB:00000% ALARM:550000000 CSQ:28 TIME:2018-07-20 14:35:00 PRESS:0.2970MPa"""

        val p = KentePacket.parse(Unpooled.buffer().writeBytes(str.toByteArray()))

        lgr.info("${JSON.toJSONString(p, true)}")
    }

    companion object {
        private val lgr = LoggerFactory.getLogger(KentePacket::class.java)
    }
}